package com.zlll.winner.controller.media;

import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.LoginUser;
import com.zlll.winner.business.model.media.MediaHost;
import com.zlll.winner.business.service.media.IMediaHostService;
import com.zlll.winner.business.service.media.IMediaMenuService;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.common.BaseResult;
import com.zlll.winner.enums.OperationEnum;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 主持人管理
 */
@LoginUser
@RestController
@RequestMapping(value = "/media/host")
public class MediaHostController extends BaseController {

    @Reference
    private IMediaHostService iMediaHostService;
    @Reference
    private IMediaMenuService iMediaMenuService;

    /**
     * 分页列表
     * @param params
     * @return
     */
    @PostMapping(value = "page")
    public Object page(@RequestBody JSONObject params){
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        Assert.notNull(pageNo,"pageNo不能为空");
        Assert.notNull(pageSize,"pageSize不能为空");
        return iMediaHostService.fetchHostListPage(params);
    }

    /**
     * 分页列表
     * @return
     */
    @PostMapping(value = "menus")
    public Object menus(@RequestBody JSONObject data){
        Integer type = data.getInteger("type");
        return iMediaMenuService.getAllMediaMenu(type);
    }

    /**
     * 新增主持人
     * @param data
     * @return
     */
    @PostMapping("add")
    public BaseResult addHost(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        MediaHost mediaHost = data.toJavaObject(MediaHost.class);
        Assert.notNull(mediaHost.getMenuId(),"栏目Id不能为空");
        boolean result = iMediaHostService.saveHost(mediaHost, getUser());
        if (result) {
            baseResult.setMsg("新增主持人失败");
            baseResult.setSuccess(false);
            return baseResult;
        }
        return baseResult;
    }

    /**
     * 编辑功能
     * @param data
     * @return
     */
    @PostMapping("edit")
    public BaseResult editAdPublish(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        MediaHost mediaHost = data.toJavaObject(MediaHost.class);
        Assert.notNull(mediaHost.getMenuId(),"栏目Id不能为空");
        Assert.notNull(mediaHost.getId(),"Id不能为空");
        iMediaHostService.editHost(mediaHost, getUser());
        return baseResult;
    }

    /**
     * 删除
     * @param data
     * @return
     */
    @PostMapping("delete")
    public BaseResult updateAdPublish(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        Long id = data.getLong("id");
        Assert.notNull(id,"主持人Id不能为空");
        String operation = data.getString("operation");
        if (OperationEnum.DELETED.getValue().equals(operation)) {
            int result = iMediaHostService.deleteHostById(id, getUser().getName());
            if (result == 0) {
                baseResult.setMsg("主持人删除失败,当前广告id为：" + id);
                baseResult.setSuccess(false);
                return baseResult;
            }
            return baseResult;
        }
        baseResult.setMsg("参数operation的值需为delete");
        baseResult.setSuccess(false);
        return baseResult;
    }

    /**
     * 查询主持人信息 根据栏目id获取
     * @return
     */
    @PostMapping(value = "hosts")
    public Object hosts(@RequestBody JSONObject data){
        Long menuId = data.getLong("menuId");
        return iMediaHostService.getMediaHostByMenuId(menuId);
    }
}
